package com.hy.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

/**
 * @Description : 三个点可以动
 * @Author : wdk
 * @CretaTime : 2020/1/17 9:44
 * @LastModify(最终修改人) :wdk
 * @LastModifyTime(最终修改时间) : 2020/1/17 9:44
 * @LastCheckBy :wdk
 */
public class LoadingPointImageView extends androidx.appcompat.widget.AppCompatImageView {

    LoadingPointDrawable loadingPointDrawable;
    int animDuration = 2000;

    public LoadingPointImageView(Context context) {
        this(context, null);
    }

    public LoadingPointImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingPointImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LoadingPointImageView);
        animDuration = ta.getInt(R.styleable.LoadingPointImageView_anim_duration, 2000);
        ta.recycle();

        loadingPointDrawable = new LoadingPointDrawable();
        loadingPointDrawable.setDuration(animDuration);
        setImageDrawable(loadingPointDrawable);
    }


    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);

        if (loadingPointDrawable != null) {
            if (visibility == VISIBLE) {
                loadingPointDrawable.start();
            } else {
                loadingPointDrawable.stop();
            }
        }
    }

    public void setSpace(float dp) {
        int size = DensityUtil.dp2px(getContext(), dp);
        if (loadingPointDrawable != null) {
            loadingPointDrawable.setSpace(size);
        }
    }

    public void setRadius(float dp) {
        int size = DensityUtil.dp2px(getContext(), dp);
        if (loadingPointDrawable != null) {
            loadingPointDrawable.setRadius(size);
        }
    }

    public void setColor(int oneColor, int towColor) {
        if (loadingPointDrawable != null) {
            loadingPointDrawable.setColor(oneColor, towColor);
        }
    }
}
